<template>
  <div class='container'>
    <el-card class="box-card">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form :inline='true' size="small" ref="form" :model="form" label-width="60px">
            <el-form-item label="关键字">
              <el-input v-model="form.keyword"></el-input>
            </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option label="已启用" value="1"></el-option>
                  <el-option label="已禁用" value="0"></el-option>
                </el-select>
              </el-form-item>
            <el-form-item>
              <el-button @click="clearSearch" type="info">清除</el-button>
              <el-button @click="getArticles" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4">
          <el-button @click.stop="addArticle" type="success">新增技巧</el-button>
        </el-col>
      </el-row>
        <el-alert
          :title="`获取文章列表成功,共${totalNum}条数据`"
          type="success">
        </el-alert>
        <el-table
          :data="videoList"
          style="width: 100%"
          class="control"
          >
          <el-table-column
            type="index"
            :index="indexMethod"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="title"
            label="文章标题"
            width="180">
              <template #default="{row}">
                {{row.title}}
                <span @click="showVideo(row.videoURL)" v-if="row.videoURL" style="color:red">
                  <i class="el-icon-film"></i>
                </span>
              </template>
          </el-table-column>
          <el-table-column
            prop="visits"
            label="阅读数"
            width="80">
          </el-table-column>
          <el-table-column
            prop="username"
            label="录入人"
            width="100"
            >
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="录入时间">
            <template #default="{row}">
              {{row.createTime | parseTimeByString}}
            </template>
          </el-table-column>
          <el-table-column
            prop="state"
            label="状态"
            width="80">
            <template #default="{row}">
              {{row.state===1? '已启用':'已禁用'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作"
            fixed="right"
            >
            <template #default="{row}">
                <el-button @click.stop="showPreview(row)" type="primary" round>预览</el-button>
                <el-button @click="toggleFbd(row.id,row.state===1?0:1)" type="success" round>
                  {{row.state===1?'禁用':'启用'}}
                </el-button>
                <el-button @click="editArticle(row.id)" :disabled="row.state===1?true:false" type="warning" round>修改</el-button>
                <el-button @click="delArticle(row)" :disabled="row.state===1?true:false" type="danger" round>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-card>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalNum">
    </el-pagination>
    <!-- 视频展示加遮罩 -->
    <div v-if="videoVisible" class="mont">
      <div class="video">
        <div @click.stop="closeVideo" class="closeVideo">
            <i class="el-icon-circle-close"></i>
        </div>
        <video id="myVideo"
          class="video-js"
          controls
          autoplay="muted"
          preload="auto">
          <source :src="videoURL" type="video/mp4" >
        </video>
      </div>
    </div>
    <!-- 添加或者修改dialog -->
      <ArticleAdd
       v-if="addVisible"
       @getArticles="getArticles"
       @closeAdd="closeAdd"
       :articleId="articleId"
       ></ArticleAdd>
    <!-- 预览文章dialog -->
      <ArticlePreview @getArticles="getArticles" @closePreview="closePreview" :dialogVisible="dialogVisible" :articleInfo="articleInfo"></ArticlePreview>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import ArticlePreview from '../components/articles-preview.vue'
import ArticleAdd from '../components/articles-add.vue'
export default {
  components: {
    ArticlePreview,
    ArticleAdd
  },
  data () {
    return {
      articleId: null,
      videoURL: '',
      addVisible: false,
      videoVisible: false,
      dialogVisible: false,
      pagesize: 10,
      page: 1,
      totalNum: 0,
      articleInfo: {},
      form: {
        keyword: null,
        state: null
      },
      videoList: [{
        id: null,
        creatorID: null,
        state: 0,
        title: '',
        username: '',
        videoURL: null,
        createTime: '',
        visits: 0,
        articleBody: ''
      }]
    }
  },
  created () {
    this.getArticles()
  },
  methods: {
    // 获取文章列表
    getArticles () {
      list({ page: this.page, pagesize: this.pagesize, keyword: this.form.keyword, state: this.form.state })
        .then(({ data }) => {
          console.log(data)
          this.videoList = data.items
          this.totalNum = data.counts
        })
    },
    // 清除搜索框
    clearSearch () {
      this.form = {
        keyword: null,
        state: null
      }
      this.getArticles()
    },
    // 获取实际序号
    indexMethod (index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    // 展示video
    showVideo (url) {
      this.videoVisible = true
      this.videoURL = url
      console.log(this.videoURL)
    },
    // 关闭video
    closeVideo () {
      this.videoVisible = false
    },
    // 展示预览
    showPreview (row) {
      this.dialogVisible = true
      this.articleInfo = row
      console.log(this.articleInfo)
    },
    // 关闭预览
    closePreview () {
      this.dialogVisible = false
    },
    // 关闭添加
    closeAdd () {
      this.addVisible = false
    },
    // 添加文章显示
    addArticle () {
      this.addVisible = true
      this.articleId = null
    },
    // 修改文章
    editArticle (id) {
      this.addVisible = true
      this.articleId = id
    },
    // 切换启用状态
    toggleFbd (id, state) {
      changeState({ id, state })
        .then(({ data }) => {
          console.log(data)
          this.getArticles()
          this.$message.success('切换成功')
        })
    },
    // 删除文章
    delArticle (data) {
      this.$confirm('此操作将永久删除文章，是否继续', '提示', {
        type: 'danger'
      }).then(async () => {
        const res = await remove(data)
        console.log(res)
        this.getArticles()
      }).catch(() => {
        this.$message.info('删除失败')
      })
    },
    // 页面
    handleSizeChange (val) {
      this.page = 1
      this.pagesize = val
      this.getArticles()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getArticles()
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style scoped lang='scss'>
.mont{
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    background: rgba(0,0,0,0.3);
    .video{
      width: 50%;
      height: 50%;
      text-align: center;
      position: relative;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      video{
        width: 100%;
        height: 80%;
      }
      .closeVideo{
        font-size: 80px;
        color: #8df;
      }
    }

}

::v-deep .control{
  .el-button{
    width: 40px;
    height: 20px;
    padding: 0;
    font-size: 12px;
  }
}
</style>
